package com.webapp.model;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;

public class CommentModel {
	
	private Statement stmt = DatabaseConnector.connect();
	
	/**
	 * Method that connects to the database and gathers all data about a comment
	 * @param commentId the comment requested
	 * @return the Comment that was requested
	 */
	public Comment getComment(int commentId){
		try{
			ResultSet rs;
			//public Comment(int post, String newContent, int newAuthor, String dater, int comment)
			rs = stmt.executeQuery("SELECT * FROM comments WHERE Comment_Id = '" +commentId+ "'");
			if(rs.next() != false){
				Time t = rs.getTime(2);
				Date d = rs.getDate(2);
				String commentDate = d.toString().concat(" "+t.toString());
				
				Comment com = new Comment (rs.getInt(4), rs.getString(3), rs.getInt(5), commentDate, rs.getInt(1));

				rs.close();
				stmt.close();
				return com;
			}
			else
				return null;
		}catch(Exception ex){return null;}
	}
	
	/*
	 * Returns a list of all comments associated to a post
	 */
	public ArrayList<Comment> getAllCommentByPost(int postId){
		ResultSet rs;
		try{
			rs = stmt.executeQuery("SELECT * FROM comments WHERE Post = '" +postId+ "'");
			ArrayList<Comment> list = new ArrayList<Comment>();
			
			while(rs.next()){
				Time t = rs.getTime(2);
				Date d = rs.getDate(2);
				String commentDate = d.toString().concat(" "+t.toString());
				
				Comment com = new Comment (rs.getInt(4), rs.getString(3), rs.getInt(5), commentDate, rs.getInt(1));
				
				list.add(com);
			}
			
			rs.close();
			return list;
			
		}catch(Exception ex){return null;}
	}
	

	/**
	 * Method that adds a comment to a post, and updates the comment count variable
	 * for that specific post
	 * @param com the comment to be added
	 * @param postId the post in which the comment will be added to
	 * @return returns TRUE if the comment was added successfully, otherwise FALSE
	 */
	public boolean commentPost(Comment com, int postId){
		try{
			ResultSet rs;
			stmt.executeUpdate("INSERT INTO comments (Comment_Id, Date, Content, Post, Author) VALUES ('"+
						com.getCommentId()+ "', '" +com.getDate()+ "', '" +com.getContent()+ "', '" +
						postId+ "', '" +com.getAuthor()+ "')");
			rs = stmt.executeQuery("SELECT Comment_Count FROM posts WHERE Post_Id = '" +postId+ "'");
			rs.next();
			
			int currentCount = rs.getInt(1);
			currentCount++;
			
			stmt.executeUpdate("UPDATE posts SET Comment_Count = '" +currentCount+ "' WHERE Post_Id = '" +postId+"'");
			rs.close();
			stmt.close();
			
		}catch(Exception ex){return false;}
		return true;
	}
	
	/**
	 * Method that checks if a comment already exists. If said comment exists,
	 * the comment Date and Content is updated
	 * @param comm the comment data which will be updated
	 * @return returns TRUE if successful, otherwise FALSE
	 */
	public boolean modifyComment(Comment comm){
		try{
			if((stmt.executeQuery("SELECT * FROM comments WHERE Comment_Id = '" +comm.getCommentId()+ "'")).next()){
				stmt.executeUpdate("UPDATE comments SET Date = '" +comm.getDate()+ "', Content = '" +comm.getContent()+
							"' WHERE Comment_Id = '" +comm.getCommentId()+ "'");
				stmt.close();
			}
			else
				return false;
		}catch(Exception ex){return false;}
		return true;
	}
	
	/**
	 * Method that deletes a comment from a specific post, and updates the comment
	 * count on that specific post.
	 * @param postId the Post that will be updated
	 * @param commentId the Comment to be removed
	 * @return returns TRUE if successful, otherwise FALSE
	 */
	public boolean deleteComment(int postId, int commentId){
		try{
			ResultSet rs;
			stmt.executeUpdate("DELETE FROM comments WHERE Comment_Id = '" +commentId+ "'");
			rs = stmt.executeQuery("SELECT Comment_Count FROM posts WHERE Post_Id = '" +postId+ "'");
			rs.next();
			
			int commentCount = rs.getInt(1);
			commentCount--;
			
			stmt.executeUpdate("UPDATE posts SET Comment_Count = '" +commentCount+ "'");
			stmt.executeUpdate("DELETE FROM user_likes_comment WHERE Comment = '" +commentId+ "'");
			rs.close();
			stmt.close();
			
		}catch(Exception ex){return false;}
		return true;
	}
	
	/**
	 * Method that adds a "like" to the user_likes_comment table, showing
	 * a user likes a specific comment.
	 * @param userId the user who liked the comment
	 * @param commentId the comment in which the user liked
	 * @return returns TRUE if successful, otherwise FALSE
	 */
	public boolean likeComment(int userId, int commentId){
		try{
			if((stmt.executeQuery("SELECT * FROM user_likes_comment WHERE User = '" +userId+ "' AND Comment = '" +commentId+ "'")).next())
				return true;
			else{
				stmt.executeUpdate("INSERT INTO user_likes_comment (User, Comment) VALUES ('" +userId+ "', '" +commentId+ "')");
				return true;
			}
		}catch(Exception ex){return false;}
	}
	
	public int numberOfLikes(int commentId){
		try{
			ResultSet rs = stmt.executeQuery("SELECT User FROM user_likes_comment WHERE Comment = '" +commentId+ "'");
			int total = 0;
			while(rs.next())
				total++;
			
			return total;
		}catch(Exception ex){return 0;}
	}
	
	/**
	 * Method that deletes a "like" from the user_likes_comment table,
	 * showing a user no longer likes a comment
	 * @param userId the user who wishes to not like a comment
	 * @param commentId the comment to be unliked
	 * @return returns TRUE if successful, otherwise FALSE
	 */
	public boolean unlikeComment(int userId, int commentId){
		try{
			if(!(stmt.executeQuery("SELECT * FROM user_likes_comment WHERE User = '" +userId+ "' AND Comment = '" +commentId+ "'")).next())
				return true;
			else{
				stmt.executeUpdate("DELETE FROM user_likes_comment WHERE User = '" +userId+ "' AND Comment = '" +commentId+ "'");
				return true;
			}
		}catch(Exception ex){return false;}
	}
}
